<template>
  <div class="login_contain">
      <div class="login_box">
          <!-- 头像部分 -->
          <div class="login_avatat">
              <img src="../assets/logo.png" alt="">
          </div>
          <!-- 登录部分 -->
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_inp">
            <!-- 登陆输入框部分 -->
            <el-form-item prop="username">
                <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" prefix-icon="el-icon-s-goods" show-password></el-input>
            </el-form-item>
            <!-- 按钮部分 -->
            <div class="btns">
                <el-button type="primary" @click="login">登录</el-button>
                <el-button type="info" @click="resetLoginForm">重置</el-button>
            </div>
          </el-form>
      </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async result => {
        if (!result) return
        const { data: loginResult } = await this.$http.post('login', this.loginForm)
        console.log(loginResult)
        if (loginResult.meta.status !== 200) return this.$message.error('登录失败，账号或密码有误')
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', loginResult.data.token)
        this.$router.push('/home')
      })
    }
  }
}

</script>

<style scoped>
.login_contain {
    background-color: #2b4b6b;
    height: 100%;
}

.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}

.login_avatat {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    border: 1px solid #eee;
    box-shadow: 0 0 10px #ddd;
    padding: 10px;
    background-color: #fff;
}

.login_avatat img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
}

.login_inp {
    position: absolute;
    bottom: 20px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}

.btns {
    display: flex;
    justify-content: flex-end;
}
</style>
